package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPlan;
import com.ruoyi.system.domain.bo.SysPlanBo;
import com.ruoyi.system.domain.vo.SysPlanVo;
import com.ruoyi.system.mapper.SysPlanMapper;
import com.ruoyi.system.service.ISysPlanService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 规划设计Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-24
 */
@RequiredArgsConstructor
@Service
public class SysPlanServiceImpl implements ISysPlanService {

    private final SysPlanMapper baseMapper;

    /**
     * 查询规划设计
     *
     * @param planId 规划设计主键
     * @return 规划设计
     */
    @Override
    public SysPlanVo queryById(Long planId){
        return baseMapper.selectVoById(planId);
    }

    /**
     * 查询规划设计列表
     *
     * @param bo 规划设计
     * @return 规划设计
     */
    @Override
    public TableDataInfo<SysPlanVo> queryPageList(SysPlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysPlan> lqw = buildQueryWrapper(bo);
        Page<SysPlanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询规划设计列表
     *
     * @param bo 规划设计
     * @return 规划设计
     */
    @Override
    public List<SysPlanVo> queryList(SysPlanBo bo) {
        LambdaQueryWrapper<SysPlan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysPlan> buildQueryWrapper(SysPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPlan> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPlanName()), SysPlan::getPlanName, bo.getPlanName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanType()), SysPlan::getPlanType, bo.getPlanType());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanNature()), SysPlan::getPlanNature, bo.getPlanNature());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanManage()), SysPlan::getPlanManage, bo.getPlanManage());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanNumber()), SysPlan::getPlanNumber, bo.getPlanNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanYear()), SysPlan::getPlanYear, bo.getPlanYear());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysPlan::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增规划设计
     *
     * @param bo 规划设计
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysPlanBo bo) {
        SysPlan add = BeanUtil.toBean(bo, SysPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPlanId(add.getPlanId());
        }
        return flag;
    }

    /**
     * 修改规划设计
     *
     * @param bo 规划设计
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysPlanBo bo) {
        SysPlan update = BeanUtil.toBean(bo, SysPlan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysPlan entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除规划设计
     *
     * @param planIds 需要删除的规划设计主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean xujianByIds(List<Long> ids) {
        List<SysPlan> planList = baseMapper.selectBatchIds(ids);
        List<SysPlan> temp = new ArrayList<>();
        planList.forEach(item->{
            SysPlan sysPlan = new SysPlan();
            BeanUtil.copyProperties(item,sysPlan,"planId");
            sysPlan.setPlanPid(item.getPlanId());
            sysPlan.setPlanType(Constants.SYS_PLAN_TYPE_XUJIAN);
            sysPlan.setStatus(Constants.SYS_PLAN_STATUS_JXZ);
            temp.add(sysPlan);
        });
        return baseMapper.insertBatch(temp);
    }
}
